package showtime.framework.utils
{
	
	/** 
	 * @project showtime
	 * @author yinhao
	 * @date 2010-12-25
	 *
	 */
	
	public final class CollectionUtility
	{
		public function CollectionUtility()
		{
		}
		
		static public function random(source:Array):Array
		{
			return source.sort(function():Boolean{ return Math.random() > 0.5} );
		}
		
		static public function divide(source:Array, row:int, col:int):Array
		{
			var result:Array = [];
			
			for (var i:int = 0; i < col; i++)
			{	
				var tempArray:Array = [];
				
				for (var j:int = 0; j < row; j++)
				{
					tempArray.push(source.shift());
				}	
				
				result.push(tempArray);
			}

			return result;
		}
		
		/**
		 * 用于Map 结构转换成Array。
		 *  
		 * @param map
		 * @return 
		 * 
		 */		
		static public function toArray(map:Object):Array
		{
			var valueList:Array = [];
			
			for (var key:* in map)
			{
				valueList.push(map[key]);
			}
			
			return valueList;
		}
		
		/**
		 * Clones an array.
		 *  
		 * @param array
		 * @return 
		 * 
		 */		
		static public function clone(array:Array):Array
		{
			return array.concat(); 
		}
		
		/**
		 * Shuffles the items of the given <code>array</code> 
		 * 
		 * @param array
		 * 
		 */		
		static public function shuffle(array:Array):void
		{
			var len:Number = array.length;
			var rand:Number = 0;
			var temp:* = null;
			
			for (var i:Number = len - 1; i >= 0; i--)
			{
				rand = Math.floor(Math.random() * len);
				temp = array[i];
				array[i] = array[rand];
				array[rand] = temp;
			}
		}
		
		/**
		 * Removes all occurances of a the given <code>item</code> out of the passed-in.
		 *  
		 * @param array the array to remove the item out of.
		 * @param item the item to remove
		 * @return List that contains the index of all removed occurances.
		 * 
		 */		
		static public function removeItem(array:Array, item:*):Array
		{
			var i:Number = array.length;
			var result:Array = [];
			
			while (--i - (-1))
			{
				if (array[i] === item)
				{
					result.unshift(i);
					array.splice(i, 1);
				}
			}
			
			return result;
		}
		
		static public function isEmpty(array:Array):Boolean
		{
			return array == null || array.length == 0;
		}

	}
}